

package com.hazelcast.spi.impl.operationservice;

import com.hazelcast.cluster.Address;
import com.hazelcast.internal.serialization.SerializationService;
import com.hazelcast.internal.server.ServerConnection;
import com.hazelcast.spi.annotation.PrivateApi;

/**
 * Allows access to certain attributes on an Operation.
 */
@PrivateApi
public final class OperationAccessor {

    private OperationAccessor() {
    }

    public static void setCallerAddress(Operation op, Address caller) {
        op.setCallerAddress(caller);
    }

    public static void setConnection(Operation op, ServerConnection connection) {
        op.setConnection(connection);
    }

    /**
     * Assigns the supplied call ID to the supplied operation, thereby activating it.
     * Refer to Operation#setCallId(long) and Operation#getCallId() for detailed semantics.
     */
    public static void setCallId(Operation op, long callId) {
        op.setCallId(callId);
    }

    /**
     * Marks the supplied operation as "not active". Refer to {@link Operation#deactivate()} for
     * detailed semantics.
     */
    public static boolean deactivate(Operation op) {
        return op.deactivate();
    }

    public static boolean hasActiveInvocation(Operation op) {
        return op.isActive();
    }

    /**
     * Sets the invocation time for the Operation.
     *
     * @param op             the Operation that is updated for its invocation time.
     * @param invocationTime the new invocation time.
     * @see Operation#setInvocationTime(long)
     */
    public static void setInvocationTime(Operation op, long invocationTime) {
        op.setInvocationTime(invocationTime);
    }

    /**
     * Sets the call timeout in milliseconds for the Operation.
     *
     * @param op          the Operation to updated for its call timeout.
     * @param callTimeout the call timeout in ms.
     * @see Operation#setCallTimeout(long)
     */
    public static void setCallTimeout(Operation op, long callTimeout) {
        op.setCallTimeout(callTimeout);
    }

    /**
     * Clone and reset the supplied operation.
     *
     * @param op                   the operation to clone and reset.
     * @param serializationService the serialization service to use for cloning.
     * @return a clone of the supplied operation, with its invocation time reset
     * to -1 and its call ID reset to 0.
     */
    public static Operation cloneAndReset(Operation op, SerializationService serializationService) {
        Operation clone = serializationService.toObject(serializationService.toData(op));
        clone.setInvocationTime(-1);
        clone.resetCallId();
        return clone;
    }

}
